package array;

/**
 * 给你一个整数数组 arr，请你判断数组中是否存在连续三个元素都是奇数的情况：如果存在，请返回 true ；否则，返回 false 。
 * <p>
 * 示例 1：
 * 输入：arr = [2,6,4,1]
 * 输出：false
 * 解释：不存在连续三个元素都是奇数的情况。
 * <p>
 * 示例 2：
 * 输入：arr = [1,2,34,3,4,5,7,23,12]
 * 输出：true
 * 解释：存在连续三个元素都是奇数的情况，即 [5,7,23] 。
 *
 * @author Jisheng Huang
 * @version 20250511
 */
public class ThreeConsecutiveOdds_1550 {

    /**
     * Loop through the array till arr.length - 2. Check whether arr[i], arr[i + 1], and arr[i + 2] are odd
     *
     * @param arr the given array
     * @return true if it exists three consecutive odds, false otherwise
     */
    public static boolean threeConsecutiveOdds(int[] arr) {
        if (arr == null || arr.length < 3) {
            return false;
        }

        for (int i = 0; i < arr.length - 2; ++i) {
            if (arr[i] % 2 == 1 && arr[i + 1] % 2 == 1 && arr[i + 2] % 2 == 1) {
                return true;
            }
        }

        return false;
    }

    /**
     * Second way to solve 1550
     *
     * @param arr the given array
     * @return true if it exists three consecutive odds, false otherwise
     */
    public static boolean threeConsecutiveOdds_2(int[] arr) {
        if (arr == null || arr.length < 3) {
            return false;
        }

        for (int i = 0; i < arr.length - 2; ++i) {
            if ((arr[i] & 1) == 1 && (arr[i + 1] & 1) == 1 && (arr[i + 2] & 1) == 1) {
                return true;
            }
        }

        return false;
    }

    public static void main(String[] args) {
        System.out.println(threeConsecutiveOdds(null));

        int[] nums = {2, 6, 4, 1};
        System.out.println(threeConsecutiveOdds(nums));

        nums = new int[]{1, 2, 34, 3, 4, 5, 7, 23, 12};
        System.out.println(threeConsecutiveOdds(nums));

        nums = new int[]{1, 1, 1};
        System.out.println(threeConsecutiveOdds(nums));

        nums = new int[]{1, 3, 2};
        System.out.println(threeConsecutiveOdds(nums));

        System.out.println();

        System.out.println(threeConsecutiveOdds_2(null));

        nums = new int[]{2, 6, 4, 1};
        System.out.println(threeConsecutiveOdds_2(nums));

        nums = new int[]{1, 2, 34, 3, 4, 5, 7, 23, 12};
        System.out.println(threeConsecutiveOdds_2(nums));

        nums = new int[]{1, 1, 1};
        System.out.println(threeConsecutiveOdds_2(nums));

        nums = new int[]{1, 3, 2};
        System.out.println(threeConsecutiveOdds_2(nums));
    }
}